/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Tools;

import org.w3c.dom.Node;

/**
 *
 * @author linhduong
 */
public class ModelGalleryEngine {

    /**
     * Search Pictures by using XPath Expression. Pictures will be searched by their name
     * @param pattern
     * @return
     */
    public static String searchPictures(String query) {
        if(query==null||query.isEmpty()) query = "\"\"";
        else query="\""+query.toLowerCase()+"\"";
        return "//Picture[contains("+ XPathEngine.toLowerCase("@src") + "," + query + ")]";
    }

    /**
     * Browse album/image inside an album
     * @param albumPATH
     * @return
     */
    public static String browseAlbum(String albumPATH) {
        if(albumPATH==null||albumPATH.isEmpty()) return null;
        String[] tmp = albumPATH.split("/");
        String query = "//Model[@id=\""+tmp[0]+"\"]/";
        for(int i=1;i<tmp.length;i++)
            query += "Album[@id=\""+tmp[i]+"\"]/";
        return query+"Picture | "+query + "Album";
    }

    /**
     * Get all model info and his/her albums
     * @param modelID
     * @return
     */
    public static String getModelInfo(String modelID) {
        return "//Model[@id=\"" + modelID + "\"]";
    }
    
    /**
     * Get the path url to a node
     * @param node
     * @return
     */
    public static String getPathUrlOf(Node node) {
        Node backTrack = node.getParentNode();
        String rel="";
        while(backTrack.getAttributes().getNamedItem("id")!=null) {
            rel = backTrack.getAttributes().getNamedItem("id").getNodeValue()+"/"+rel;
            backTrack = backTrack.getParentNode();
        }
        return rel;

    }
}
